<?php

class Publish_AttachmentController extends Ext_Controller_Action
{
    public function init()
    {
        Zend_Registry::set('_use_adapter', 'master');
        parent::init();
    }
    
    public function saveImageAction()
    {
        $viewResult = new Ext_View_Result();
        $viewResult->setLayout("layout_result-xml");
        
        $settings = Zend_Registry::get('settings');
        if(!$settings['allowUploadImage']){
            $viewResult->addMessages('Access denied.');
            $viewResult->setNoRender();
            return "";
        }
        
        $articleId = $this->filter->filter($this->_request->getPost('articleId'));

        $validater = new Ext_Validate();
        $validater
            ->addValidater(new Add_Publish_Article_SavePost_Validate_Acl($articleId))
        ;
        if(!$validater->isValid()){
            $viewResult->addMessages($validater->getMessages());
            $viewResult->setNoRender();
            return "";
        }

        $uploador = new Ext_Image_Upload();
        $uploadResultor = $uploador->upload();
        if(!$uploadResultor->isValid()){
            $viewResult->addMessages($uploadResultor->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $uploadFile = $uploadResultor->getData()->file;
        $uploadName = $uploadResultor->getData()->name;
        
        $savor = new Ext_Image_Save();
        $saveResultor = $savor->save($uploadFile);
        if(!$saveResultor->isValid()){
            $viewResult->addMessages($saveResultor->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $imageId = $saveResultor->getData();
        
        $data = new stdClass();
        $data->imageId = $imageId;
        $data->title   = $uploadName;
        
        $imageAddor = new Ext_Attachment_Image_Add($articleId);
        $addResultor = $imageAddor->add($data);
        if(!$addResultor->isValid()){
            $viewResult->addMessages($addResultor->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $attachmentImageId = $addResultor->getData();
        
        $this->view->attachmentImage = new Ext_Attachment_Image_Show($attachmentImageId);
        $viewResult->addMessage($this->translater->_("Submit success."));
        $viewResult->setResult(1);
        return "";
    }
    
    public function saveVideoAction()
    {
        $viewResult = new Ext_View_Result();

        $settings = Zend_Registry::get('settings');
        if(!$settings['allowShareVideo']){
            $viewResult->addMessages('Access denied.');
            $viewResult->setNoRender();
            return "";
        }
        
        $articleId = $this->filter->filter($this->_request->getPost('articleId'));
        $videoUrl  = $this->filter->filter($this->_request->getPost('videoUrl'));

        $validater = new Ext_Validate();
        $validater
            ->addValidater(new Add_Publish_Article_SavePost_Validate_Acl($articleId))
        ;
        if(!$validater->isValid()){
            $viewResult->addMessages($validater->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $fetcher = new Ext_Video_Fetch($videoUrl);
        $fetchResulter = $fetcher->fetch();
        if(!$fetchResulter->isValid())
        {
            $viewResult->addMessages($fetchResulter->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $datas = $fetchResulter->getDatas();
        $title = $datas['title'];
        
        $videoSaver = new Ext_Video_Save();
        $videoSaveResulter = $videoSaver->save($datas);
        if(!$videoSaveResulter->isValid())
        {
            $viewResult->addMessages($videoSaveResulter->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $videoId = $videoSaveResulter->getVideoId();
        
        $data = new stdClass();
        $data->videoId = $videoId;
        $data->title   = $title;
        $adder = new Ext_Attachment_Video_Add($articleId);
        $addResulter = $adder->add($data);
        if(!$addResulter->isValid())
        {
            $viewResult->addMessages($addResulter->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $attachmentVideoId = $addResulter->getData();

        $data = array();
        $data[] = new Ext_Attachment_Video_Show($attachmentVideoId);
        $viewResult->setData($data);
        $viewResult->addMessage($this->translater->_("Submit success."));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
        return "";
    }
    
    public function saveMusicAction()
    {
        $viewResult = new Ext_View_Result();
        
        $settings = Zend_Registry::get('settings');
        if(!$settings['allowShareMusic']){
            $viewResult->addMessages('Access denied.');
            $viewResult->setNoRender();
            return "";
        }
        
        $articleId = $this->filter->filter($this->_request->getPost('articleId'));
        $musicUrl = $this->filter->filter($this->_request->getPost('musicUrl'));

        $validater = new Ext_Validate();
        $validater
            ->addValidater(new Add_Publish_Article_SavePost_Validate_Acl($articleId))
        ;
        if(!$validater->isValid()){
            $viewResult->addMessages($validater->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $fetcher = new Ext_Music_Fetch($musicUrl);
        $fetchResultor = $fetcher->fetch();
        if(!$fetchResultor->isValid())
        {
            $viewResult->addMessages($fetchResultor->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $datas = $fetchResultor->getDatas();
        $name   = $datas['name'];
        $singer = $datas['singer'];
        
        $musicSaver = new Ext_Music_Save();
        $musicSaveResulter = $musicSaver->save($datas);
        if(!$musicSaveResulter->isValid())
        {
            $result['messages'] = $musicSaveResulter->getMessages();
            return new Ext_Publish_Attachment_Add_Result($result['code'], $result['datas'], $result['messages']);
        }
        $musicId = $musicSaveResulter->getData();
        
        $data = new stdClass();
        $data->musicId = $musicId;
        $data->name    = $name;
        $data->singer  = $singer;
        $adder = new Ext_Attachment_Music_Add($articleId);
        $addResulter = $adder->add($data);
        if(!$addResulter->isValid())
        {
            $viewResult->addMessages($addResulter->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $attachmentMusicId = $addResulter->getData();

        $data = array();
        $data[] = new Ext_Attachment_Music_Show($attachmentMusicId);
        $viewResult->setData($data);
        $viewResult->addMessage($this->translater->_("Submit success."));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
        return "";
    }
    
    public function saveShoppingAction()
    {
        $viewResult = new Ext_View_Result();

        $settings = Zend_Registry::get('settings');
        if(!$settings['allowShareVideo']){
            $viewResult->addMessages('Access denied.');
            $viewResult->setNoRender();
            return "";
        }
        
        $articleId = $this->filter->filter($this->_request->getPost('articleId'));
        $shoppingUrl  = $this->filter->filter($this->_request->getPost('shoppingUrl'));

        $validater = new Ext_Validate();
        $validater
            ->addValidater(new Add_Publish_Article_SavePost_Validate_Acl($articleId))
        ;
        if(!$validater->isValid()){
            $viewResult->addMessages($validater->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $fetcher = new Ext_Shopping_Fetch($shoppingUrl);
        $fetchResulter = $fetcher->fetch();
        if(!$fetchResulter->isValid())
        {
            $viewResult->addMessages($fetchResulter->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $datas = $fetchResulter->getDatas();
        $name  = $datas['name'];
        
        $shoppingSaver = new Ext_Shopping_Save();
        $shoppingSaveResulter = $shoppingSaver->save($datas);
        if(!$shoppingSaveResulter->isValid())
        {
            $viewResult->addMessages($shoppingSaveResulter->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $shoppingId = $shoppingSaveResulter->getData();
        
        $data = new stdClass();
        $data->shoppingId = $shoppingId;
        $data->name       = $name;
        $adder = new Ext_Attachment_Shopping_Add($articleId);
        $addResulter = $adder->add($data);
        if(!$addResulter->isValid())
        {
            $viewResult->addMessages($addResulter->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $attachmentShoppingId = $addResulter->getData();

        $this->view->attachmentShopping = new Ext_Attachment_Shopping_Show($attachmentShoppingId);
        $viewResult->addMessage($this->translater->_("Submit success."));
        $viewResult->setResult(1);
        return "";
    }

    public function deleteAttachmentAction()
    {
        $viewResult = new Ext_View_Result();
        
        $articleId   = $this->filter->filter($this->_request->get('articleId'));
        $_attachment = $this->filter->filter($this->_request->get('attachment'));
        
        $validater = new Ext_Validate();
        $validater
            ->addValidater(new Add_Publish_Article_SavePost_Validate_Acl($articleId))
        ;
        if(!$validater->isValid()){
            $viewResult->addMessages($validater->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $_attachmentArray = explode('_', $_attachment);
        $attachmentType   = isset($_attachmentArray['0']) ? $_attachmentArray['0'] : '';
        $attachmentDataId = isset($_attachmentArray['1']) ? $_attachmentArray['1'] : '';

        $_deleter = new Ext_Publish_Attachment_Delete($articleId);
        $_resulter = $_deleter->delete($attachmentType, $attachmentDataId);
        if(!$_resulter->isValid()){
            $viewResult->addMessages($_resulter->getMessages());
            $viewResult->setNoRender();
            return "";
        }

        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }

    public function saveAttachmentsOrderAction()
    {
        $viewResult = new Ext_View_Result();
        
        $articleId = $this->filter->filter($this->_request->get('articleId'));
        $_newOrder = $this->_request->get('newOrder');
        
        $validater = new Ext_Validate();
        $validater
            ->addValidater(new Add_Publish_Article_SavePost_Validate_Acl($articleId))
        ;
        if(!$validater->isValid()){
            $viewResult->addMessages($validater->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $_newOrder2 = array();
        for($i=0;$i<count($_newOrder);$i++)
        {
            $_orderValue = $_newOrder[$i];
            $_orderArray = explode("_", $_orderValue);
            
            $_attachmentType   = $_orderArray["0"];
            $_attachmentDataId = $_orderArray["1"];
            
            $_newOrder2[$i] = array("type"=>$_attachmentType, "id"=>$_attachmentDataId);
        }

        $_order = new Ext_Publish_Attachment_Order($articleId);
        $_resulter = $_order->order($_newOrder2);
        if(!$_resulter->isVAlid()){
            $viewResult->addMessages($_resulter->getMessages());
            $viewResult->setNoRender();
            return "";
        }

        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }

}

?>